package com.example.demo1.service.impl.service.impl;

import com.example.demo1.common.ResponseCode;
import com.example.demo1.common.ServerResponse;
import com.example.demo1.constant.Constants;
import com.example.demo1.entity.BaRole;
import com.example.demo1.entity.BaUser;
import com.example.demo1.entity.BaUserRole;
import com.example.demo1.entity.BaUserRoleKey;
import com.example.demo1.mapper.BaRoleMapper;
import com.example.demo1.mapper.BaUserMapper;
import com.example.demo1.mapper.BaUserRoleMapper;
import com.example.demo1.service.impl.service.UserService;
import com.example.demo1.util.JwtUtil;
import com.example.demo1.util.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private BaUserMapper userMapper;

    @Autowired
    private BaUserRoleMapper userRoleMapper;
    
    @Autowired
    private BaRoleMapper crmRoleMapper;

    @Override
    public ServerResponse<String> doLogin(String userName, String password) {
        logger.info("时间：" + new Date() + "=================UserServiceImpl.doLogin方法调用开始");
        if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)){
            logger.info("时间：" + new Date() + "=================UserServiceImpl.doLogin方法参数错误");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        BaUser user = userMapper.checkUserName(userName);
        if(user == null){
            logger.info("时间：" + new Date() + "=================UserServiceImpl.doLogin方法用户名不存在");
            return ServerResponse.createByErrorMessage("用户名不存在");
        }
        if(MD5Util.MD5Encode(Constants.profiles + password, "UTF-8").equals(user.getUserPasswd())){
            String token;
            try {
                token = JwtUtil.createToken(user.getUserId()+"", JwtUtil.TOKEN_EXP_TIME_2H);
            } catch (Exception e) {
                e.printStackTrace();
                logger.info("时间：" + new Date() + "=================UserServiceImpl.doLogin方法token生成异常");
                return ServerResponse.createByErrorCodeMessage(ResponseCode.CREATE_TOKEN_ERROR.getCode(), "token生成异常");
            }
            logger.info("时间：" + new Date() + "=================UserServiceImpl.doLogin方法成功");
            return ServerResponse.createBySuccess(token);
        }
        logger.info("时间：" + new Date() + "=================UserServiceImpl.doLogin方法密码错误");
        return ServerResponse.createByErrorMessage("密码错误");

    }


    @Override
    public ServerResponse doLogOut(Integer userId) {
        logger.info("时间：" + new Date() + "=================UserServiceImpl.doLogOut方法调用开始");
        if(userId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        logger.info("时间：" + new Date() + "=================UserServiceImpl.doLogOut方法成功");
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse<BaUser> getUserInfo(Integer userId) {
        logger.info("时间：" + new Date() + "=================UserServiceImpl.getUserInfo方法调用开始");
        if(userId == null){
            logger.info("时间：" + new Date() + "=================UserServiceImpl.getUserInfo方法参数错误");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        BaUser user = userMapper.selectByPrimaryKey(userId);
        if(user == null){
            logger.info("时间：" + new Date() + "=================UserServiceImpl.getUserInfo方法用户信息获取失败");
            return ServerResponse.createByErrorMessage("用户信息获取失败");
        }
        logger.info("时间：" + new Date() + "=================UserServiceImpl.getUserInfo方法成功");
        return ServerResponse.createBySuccess(user);
    }

    @Override
    public ServerResponse qryUsersForPT(String userName, int pageNum, int pageSize) {
        logger.info("时间：" + new Date() + "=================UserServiceImpl.qryUsersForPT方法调用开始");
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> users = userMapper.selectUsersByUserNameForPT(userName);
        if(!CollectionUtils.isEmpty(users)){
            for(Map<String, Object> userItem : users){
                List<Integer> userRoleIds = userRoleMapper.selectRoleIdsByUserId(Integer.parseInt(String.valueOf(userItem.get("userId"))));
                if(CollectionUtils.isEmpty(userRoleIds)){
                    userRoleIds = new ArrayList<>();
                }
                userItem.put("userRoleIds", userRoleIds);
            }
        }
        PageInfo pageInfo = new PageInfo(users);
        pageInfo.setList(users);
        logger.info("时间：" + new Date() + "=================UserServiceImpl.qryUsersForPT方法执行成功");
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse addUserInfoForPT(BaUser user, Integer userId, String roleIds) {
        logger.info("时间：" + new Date() + "=================UserServiceImpl.addUserInfoForPT方法调用开始");
        if(user == null || roleIds == null){
            logger.info("时间：" + new Date() + "=================UserServiceImpl.addUserInfoForPT方法参数校验不通过");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        if (userMapper.selectUserByUserName(user.getUserName()) != null){
            logger.error("时间：" + new Date() + "===================UserServiceImpl.addUserInfoForPT方法用户名已存在");
            return ServerResponse.createByErrorMessage("用户名已存在,请重新填写");
        }
        if (userMapper.validateUserEmail(user.getUserMail()) != null){
            logger.error("时间：" + new Date() + "===================UserServiceImpl.addUserInfoForPT方法邮箱已存在");
            return ServerResponse.createByErrorMessage("邮箱已存在,请重新填写");
        }
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        user.setCreateTime(format.format(date));
        user.setUpdateTime(format.format(date));
        user.setUserPasswd(MD5Util.MD5Encode(Constants.profiles + user.getUserPasswd(), "UTF-8"));
        int insertNum = userMapper.insert(user);
        int insertUserRoleNum;
        BaUserRole userRole;
        if(insertNum > 0){
            if(!StringUtils.isEmpty(roleIds)){
                List<String> roleIdList = Arrays.asList(roleIds.split(",",-1));
                if(!CollectionUtils.isEmpty(roleIdList)){
                    for(String roleId : roleIdList){
                        userRole = new BaUserRole();
                        userRole.setRoleId(Integer.valueOf(roleId));
                        userRole.setUserId(user.getUserId());
                        userRole.setUpdateUser(userId.toString());
                        userRole.setUpdateTime(new Date());
                        insertUserRoleNum = userRoleMapper.insert(userRole);
                        if(insertUserRoleNum <= 0){
                            logger.error("时间：" + new Date() + "===================UserServiceImpl.addUserInfoForPT方法添加用户角色关联信息出错");
                            throw new RuntimeException("时间：" + new Date() + "===================UserServiceImpl.addUserInfoForPT方法添加用户角色关联信息出错");
                        }
                    }
                }
            }
            logger.info("时间：" + new Date() + "===================UserServiceImpl.addUserInfoForPT方法执行成功");
            return ServerResponse.createBySuccess();
        }
        logger.error("时间：" + new Date() + "===================UserServiceImpl.addUserInfoForPT方法添加用户信息失败");
        return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }

    @Override
    public ServerResponse addUserInfo(BaUser user,Integer userId) {
        logger.info("时间：" + new Date() + "=================UserServiceImpl.addUserInfo方法调用开始");
        if(user == null){
            logger.info("时间：" + new Date() + "=================UserServiceImpl.addUserInfo方法参数校验不通过");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        if (userMapper.selectUserByUserName(user.getUserName()) != null){
            logger.error("时间：" + new Date() + "===================UserServiceImpl.addUserInfo方法用户名已存在");
            return ServerResponse.createByErrorMessage("用户名已存在,请重新填写");
        }
        if (userMapper.validateUserEmail(user.getUserMail()) != null){
            logger.error("时间：" + new Date() + "===================UserServiceImpl.addUserInfo方法邮箱已存在");
            return ServerResponse.createByErrorMessage("邮箱已存在,请重新填写");
        }
        List<BaRole> crmRoles = crmRoleMapper.getAllRoles();
        Integer roleId = null;
        if (!CollectionUtils.isEmpty(crmRoles)) {
            if (user.getIsManager() == 1) {
                for (BaRole crmRole : crmRoles) {
                    if ("企业普通用户".equals(crmRole.getRoleName())) {
                        roleId = crmRole.getRoleId();
                    }
                }
            } else if (user.getIsManager() == 0) {
                for (BaRole crmRole : crmRoles) {
                    if ("企业部门管理员".equals(crmRole.getRoleName())) {
                        roleId = crmRole.getRoleId();
                    }
                }
            }
            if (roleId == null) {
                logger.error("时间：" + new Date() + "===================UserServiceImpl.addUserInfo方法角色信息获取失败");
                return ServerResponse.createByErrorMessage("角色信息获取失败");
            }
        } else {
            logger.error("时间：" + new Date() + "===================UserServiceImpl.addUserInfo方法角色信息获取失败");
            return ServerResponse.createByErrorMessage("角色信息获取失败");
        }
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        user.setCreateTime(format.format(date));
        user.setUpdateTime(format.format(date));
        user.setUserPasswd(MD5Util.MD5Encode(Constants.profiles + user.getUserPasswd(), "UTF-8"));
        int insertNum = userMapper.insert(user);
        if (insertNum != 1) {
            logger.error("时间：" + new Date() + "===================UserServiceImpl.addUserInfo方法添加用户信息失败");
            return ServerResponse.createByErrorMessage("添加用户信息失败");
        }
        BaUserRole userRole = new BaUserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(user.getUserId());
        userRole.setCreateUser(userId.toString());
        userRole.setCreateTime(new Date());
        int insertUserRoleNum = userRoleMapper.insert(userRole);
        if(insertUserRoleNum != 1){
            logger.error("时间：" + new Date() + "===================UserServiceImpl.addUserInfo方法添加用户角色关联信息出错");
            throw new RuntimeException("添加用户角色关联信息出错");
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse editUserInfoForPT(BaUser user, Integer userId, String roleIds) {
        logger.info("时间：" + new Date() + "=================UserServiceImpl.editUserInfoForPT方法调用开始");
        if(user == null || roleIds == null){
            logger.info("时间：" + new Date() + "=================UserServiceImpl.editUserInfoForPT方法参数校验不通过");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        if (userMapper.selectUserByUserNameNotSelf(user.getUserName(), user.getUserId()) != null){
            logger.error("时间：" + new Date() + "===================UserServiceImpl.editUserInfoForPT方法用户名已存在");
            return ServerResponse.createByErrorMessage("用户名已存在,请重新填写");
        }
        if (userMapper.validateUserEmailNotSelf(user.getUserMail(), user.getUserId()) != null){
            logger.error("时间：" + new Date() + "===================UserServiceImpl.editUserInfoForPT方法邮箱已存在");
            return ServerResponse.createByErrorMessage("邮箱已存在,请重新填写");
        }
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        user.setUpdateTime(format.format(date));
        Integer editNum = userMapper.updateByPrimaryKeySelective(user);
        Integer insertUserRoleNum;
        BaUserRole userRole;
        if(editNum > 0){
            int deleteNum = userRoleMapper.deleteByUserId(user.getUserId());
            if(deleteNum < 0){
                throw new RuntimeException("时间：" + new Date() + "===================UserServiceImpl.editUserInfoForPT方法更新用户角色信息失败");
            }
            if(!StringUtils.isEmpty(roleIds)){
                List<String> roleIdList = Arrays.asList(roleIds.split(",",-1));
                if(!CollectionUtils.isEmpty(roleIdList)){
                    for(String roleId : roleIdList){
                        userRole = new BaUserRole();
                        userRole.setRoleId(Integer.valueOf(roleId));
                        userRole.setUserId(user.getUserId());
                        userRole.setUpdateUser(userId.toString());
                        userRole.setUpdateTime(new Date());
                        insertUserRoleNum = userRoleMapper.insert(userRole);
                        if(insertUserRoleNum <= 0){
                            logger.error("时间：" + new Date() + "===================UserServiceImpl.editUserInfoForPT方法添加用户角色关联信息出错");
                            throw new RuntimeException("时间：" + new Date() + "===================UserServiceImpl.editUserInfoForPT方法添加用户角色关联信息出错");
                        }
                    }
                }
            }
            logger.info("时间：" + new Date() + "===================UserServiceImpl.editUserInfoForPT方法执行成功");
            return ServerResponse.createBySuccess();
        }

        logger.error("时间：" + new Date() + "====================UserServiceImpl.editUserInfoForPT方法执行失败");
        return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }

    @Override
    public ServerResponse editUserInfo(BaUser user, Integer userId) {
        logger.info("时间：" + new Date() + "=================UserServiceImpl.editUserInfo方法调用开始");
        if(user == null){
            logger.info("时间：" + new Date() + "=================UserServiceImpl.editUserInfo方法参数校验不通过");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        if (userMapper.selectUserByUserNameNotSelf(user.getUserName(), user.getUserId()) != null){
            logger.error("时间：" + new Date() + "===================UserServiceImpl.editUserInfo方法用户名已存在");
            return ServerResponse.createByErrorMessage("用户名已存在,请重新填写");
        }
        if (userMapper.validateUserEmailNotSelf(user.getUserMail(), user.getUserId()) != null){
            logger.error("时间：" + new Date() + "===================UserServiceImpl.editUserInfo方法邮箱已存在");
            return ServerResponse.createByErrorMessage("邮箱已存在,请重新填写");
        }
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        user.setUpdateTime(format.format(date));
        Integer editNum = userMapper.updateByPrimaryKeySelective(user);
        if (editNum != 1) {
            logger.error("时间：" + new Date() + "====================UserServiceImpl.editUserInfo方法更新失败");
            return ServerResponse.createByErrorMessage("更新失败");
        }
        int deleteNum = userRoleMapper.deleteByUserId(user.getUserId());
        if(deleteNum < 0){
            throw new RuntimeException("删除用户原角色信息失败");
        }
        List<BaRole> crmRoles = crmRoleMapper.getAllRoles();
        Integer roleId = null;
        if (!CollectionUtils.isEmpty(crmRoles)) {
            if (user.getIsManager() == 1) {
                for (BaRole crmRole : crmRoles) {
                    if ("企业普通用户".equals(crmRole.getRoleName())) {
                        roleId = crmRole.getRoleId();
                    }
                }
            } else if (user.getIsManager() == 0) {
                for (BaRole crmRole: crmRoles) {
                    if ("企业部门管理员".equals(crmRole.getRoleName())) {
                        roleId = crmRole.getRoleId();
                    }
                }
            }
            if (roleId == null) {
                logger.error("时间：" + new Date() + "===================UserServiceImpl.editUserInfo方法角色信息获取失败");
                throw new RuntimeException("角色信息获取失败");
            }
        } else {
            logger.error("时间：" + new Date() + "===================UserServiceImpl.editUserInfo方法角色信息获取失败");
            throw new RuntimeException("角色信息获取失败");
        }
        BaUserRole userRole = new BaUserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(user.getUserId());
        userRole.setCreateUser(userId.toString());
        userRole.setCreateTime(new Date());
        int insertUserRoleNum = userRoleMapper.insert(userRole);
        if(insertUserRoleNum != 1){
            logger.error("时间：" + new Date() + "===================UserServiceImpl.addUserInfo方法添加用户角色关联信息出错");
            throw new RuntimeException("添加用户角色关联信息出错");
        }
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse deleteUserInfoForPT(String userIds) {
        logger.info("时间：" + new Date() + "=================UserServiceImpl.deleteUserInfoForPT方法调用开始");
        if(StringUtils.isEmpty(userIds)){
            logger.error("时间：" + new Date() + "=================UserServiceImpl.deleteUserInfoForPT方法参数校验不通过");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        List<String> userIdList = Arrays.asList(userIds.split(",",-1));
        if(CollectionUtils.isEmpty(userIdList)){
            logger.error("时间：" + new Date() + "=================UserServiceImpl.deleteUserInfoForPT方法参数截取失败");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
        }
        Integer deleteNum;
        for(String userId : userIdList){
            List<Integer> roleIds = userRoleMapper.selectRoleIdsByUserId(Integer.valueOf(userId));
            if(!CollectionUtils.isEmpty(roleIds) && !(roleIds.size() == 0)){
                logger.error("时间：" + new Date() + "=================UserServiceImpl.deleteUserInfoForPT方法将删除的用户还关联角色信息");
                return ServerResponse.createByErrorMessage("该用户还关联有角色信息,请解除关联后重试");
            }
            deleteNum = userMapper.deleteByPrimaryKey(Integer.valueOf(userId));
            if(deleteNum <= 0){
                logger.error("时间：" + new Date() + "=================UserServiceImpl.deleteUserInfoForPT方法删除角色信息失败");
                throw new RuntimeException("时间：" + new Date() + "=================UserServiceImpl.deleteUserInfoForPT方法删除角色信息失败");
            }
        }
        logger.info("时间：" + new Date() + "===================UserServiceImpl.deleteUserInfoForPT方法执行成功");
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse deleteUserInfo(String userIds) {
        logger.info("时间：" + new Date() + "=================UserServiceImpl.deleteUserInfo方法调用开始");
        if(StringUtils.isEmpty(userIds)){
            logger.error("时间：" + new Date() + "=================UserServiceImpl.deleteUserInfo方法参数校验不通过");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        List<String> userIdList = Arrays.asList(userIds.split(",",-1));
        if(CollectionUtils.isEmpty(userIdList)){
            logger.error("时间：" + new Date() + "=================UserServiceImpl.deleteUserInfo方法参数截取失败");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
        }
        for(String userId : userIdList){
            List<Integer> roleIds = userRoleMapper.selectRoleIdsByUserId(Integer.valueOf(userId));
            Integer deleteNum = userMapper.deleteByPrimaryKey(Integer.valueOf(userId));
            if(deleteNum != 1){
                logger.error("时间：" + new Date() + "=================UserServiceImpl.deleteUserInfo方法删除用户信息失败");
                throw new RuntimeException("删除用户信息失败");
            }
            if (!CollectionUtils.isEmpty(roleIds)) {
                for (Integer roleId : roleIds) {
                    BaUserRoleKey BaUserRoleKey = new BaUserRoleKey();
                    BaUserRoleKey.setRoleId(roleId);
                    BaUserRoleKey.setUserId(Integer.parseInt(userId));
                    int deleteUserRoleNum = userRoleMapper.deleteByPrimaryKey(BaUserRoleKey);
                    if (deleteUserRoleNum != 1) {
                        logger.error("时间：" + new Date() + "=================UserServiceImpl.deleteUserInfo方法删除用户关联角色信息失败");
                        throw new RuntimeException("删除用户关联角色信息失败");
                    }
                }
            }
        }
        logger.info("时间：" + new Date() + "===================UserServiceImpl.deleteUserInfo方法执行成功");
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse selectUserRoleByUserId(String userId) {
        ServerResponse response = null ;
        if(StringUtils.isEmpty(userId)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),"参数错误") ;
        }

        try{
            Integer role = userMapper.selectUserRoleByUserId(Integer.valueOf(userId));
            response = ServerResponse.createBySuccess(role) ;
        }catch (Exception ex){
            ex.printStackTrace();
            return ServerResponse.createByError() ;
        }
        return response;
    }

    /**
     * 个人中心-数据接口
     * @param userId
     * @return
     */
    @Override
    public ServerResponse getUserCenterInfo(Integer userId) {

        logger.info("时间：" + new Date() + "=====================调用SalesServiceImpl.getUserCenterInfo方法调用开始");

        if (userId == null) {
            logger.info("时间：" + new Date() + "=====================调用SalesServiceImpl.getUserCenterInfo方法参数错误");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }

        BaUser userCenterInfo = userMapper.selectByPrimaryKey(userId);

        logger.info("时间：" + new Date() + "=====================调用SalesServiceImpl.getUserCenterInfo方法成功");

        return ServerResponse.createBySuccess(userCenterInfo);
    }

    /**
     * 个人中心-修改密码
     * @param userId
     * @param originPasswd
     * @param newPasswd
     * @return
     */
    @Override
    public ServerResponse userCenterUpdatePasswd(Integer userId, String originPasswd, String newPasswd) {

        logger.info("时间：" + new Date() + "=====================调用SalesServiceImpl.userCenterUpdatePasswd方法调用开始");

        if (userId == null || org.springframework.util.StringUtils.isEmpty(originPasswd) || org.springframework.util.StringUtils.isEmpty(newPasswd)) {
            logger.info("时间：" + new Date() + "=====================调用SalesServiceImpl.userCenterUpdatePasswd方法参数错误");
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }

        BaUser originUserInfo = userMapper.selectByPrimaryKey(userId);
        if (originUserInfo == null) {
            logger.info("时间：" + new Date() + "=====================调用SalesServiceImpl.userCenterUpdatePasswd方法用户信息获取失败");
            return ServerResponse.createByErrorMessage("用户信息获取失败");
        }

        if (!MD5Util.MD5Encode(Constants.profiles + originPasswd, "UTF-8").equals(originUserInfo.getUserPasswd())) {
            logger.info("时间：" + new Date() + "=====================调用SalesServiceImpl.userCenterUpdatePasswd方法原密码错误");
            return ServerResponse.createByErrorMessage("原密码错误");
        }

        BaUser toUpdateUser = new BaUser();
        toUpdateUser.setUserId(userId);
        toUpdateUser.setUserPasswd(MD5Util.MD5Encode(Constants.profiles + newPasswd, "UTF-8"));
        toUpdateUser.setUpdateUser(userId);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        toUpdateUser.setUpdateTime(format.format(date));
        int updateResult = userMapper.updateByPrimaryKeySelective(toUpdateUser);
        if (updateResult != 1) {
            logger.info("时间：" + new Date() + "=====================调用SalesServiceImpl.userCenterUpdatePasswd方法密码修改失败");
            return ServerResponse.createByErrorMessage("密码修改失败");
        }

        logger.info("时间：" + new Date() + "=====================调用SalesServiceImpl.userCenterUpdatePasswd方法成功");

        return ServerResponse.createBySuccess();
    }

}
